<?php
if ($_SESSION['login_user']['status'] != 'admin' || LZ_MODULE != 'admin') die('Access Denied');
if($_SESSION['login_user']['status'] != 'admin')
{
	lz_exit(ACCESS_DENIED,'admin.php',1);
}
$action = $_GET['action'];
$m = $_GET['m'];

$album_id = intval($_GET['album_id']);

include_once('model/photo_album.php');
$album = new LZ_Photo_Album;



if ($m == 'new_album')
{
	$data = filter_array($_POST,'name!,description');
	if ($data)
	{
		if ( $album->add( $data ) )
		{
			lz_exit(ALBUM_NEW_SUCCESS,'admin.php?p=photo_album',1);
		}
		else
		{
			$action = 'new_album';
			$err_msg = ALBUM_NEW_ERROR;
		}
	}
	else
	{
		$action = 'new_album';
		$err_msg = ALBUM_FILL_ALL;
		$view_data['album'] = $_POST;
	}
}
else if ($m == 'add')
{
	$ids = $_POST['ids'];
	$name = $_POST['name'];
	include_once('model/photo_album.php');
	$photo_album = new LZ_Photo_Album;
	if ($id = $photo_album->exists($name))
	{
		$data = $photo_album->get_one($id);
		$ids_arr = explode(',',$data['photo_ids']);
		$new_ids_arr = explode(',',$ids);
		foreach($new_ids_arr as $_id)
		{
			$_id = trim($_id);
			if (!$_id) continue;
			$ids_arr[] = $_id;
		}
		$ids_arr = array_unique($ids_arr);
		$data['photo_ids'] = join(',',$ids_arr);
		
		if ($photo_album->update($id,$data))
			echo $id.'success';
		else
			echo 'error';
	}
	else
	{
		if ($_id = $photo_album->add(array(
			'name'=>$name,
			'photo_ids'=>$ids
		)))
		{
			echo $_id.'success';
		}
		else
			echo 'error';
	}
	die;
}
else if ($m == 'update' && intval($album_id))
{
	$ids = $_POST['ids'];
	$data = array('photo_ids' => $ids);
	if ($album->update($album_id,$data))
		echo 'success';
	else
		echo 'error';
	die;
}


//page

$total = $album->get_list( array('select' => 'count(*)') );
$total = $total[0]['count(*)'];
$page = intval($_GET['page']);
$per_page = (intval($config['albums_per_page']))?intval($config['albums_per_page']):30;
!$page && $page=1;
$total_page = ceil($total/$per_page);
!$total_page && $total_page=1;
$page>$total_page && $page = $total_page;
$albums = $album->get_list(array('from' => ($page-1)*$per_page,'total'=>$per_page ));
$view_data['page'] = $page;

$pager = PAGER_TOTAL.$total.PAGER_ITEMS.$total_page.PAGER_PAGE.'<br />';
$pager.= html_pager("admin.php?p=photo_album",$total_page,$page);
$view_data['pager'] = $pager;
$view_data['page_description'] = ALBUMS_LIST;

if ($action == 'add')
{
	$_temp = template('photo_album_add.html');
	$_temp->assign('albums',$albums);
	$_temp->output();
	die;
}
elseif ($action == "sort")
{
	$album_id = intval($_GET['album_id']);
	if ($album_id)
	{
		$view_data['action'] = 'sort';
		$view_data['this_album'] = $album->get_one($album_id);
		$arr = explode(',',$view_data['this_album']['photo_ids']);
		$images = array();
		include_once('model/photo.php');
		$photo = new LZ_Photo;
		foreach($arr as $id)
		{
			$id = intval($id);
			if (!$id) continue;
			$img = $photo->get_one($id);
			if ($img) $images[] = $img;
		}
		$view_data['album_images'] = $images;
		$view_data['page_description'] = $view_data['this_album']['name'];
	}
}

$_SESSION['last_list_page'] = "admin.php?p=photo_album&album_id=$album_id&action=sort";



for($i=0;$i<count($albums);$i++)
{
	if ($albums[$i]['status'] == 'hidden') $albums[$i]['status_translated'] = ALBUM_HIDDEN;
	else
	{
		$albums[$i]['status_translated'] = ALBUM_PUBLIC;
		$albums[$i]['status'] = 'public';
	}
}
$view_data['albums'] = $albums;

$view_data['real_content'] = "album_new.html";
$view_data['err_msg'] = $err_msg;
$view_data['success_msg'] = $success_msg;
$view_data['album_id'] = $album_id;
?>